import Cesium from '../Ces/Cesium';
import WeLengthways from './WeLengthways';
import WeDisplayParameter from './WeDisplayParameter';
import WeLengthwaysInRect from './WeLengthwaysInRect';

class WeLengthwaysManager {
  // Member variables
  rectangle;
  _display;
  _dataProvider;
  extrudedHeight;
  _lengthways;
  _grid;
  _scaleZ;
  _sectPositionX;
  _sectPositionY;

  constructor(options) {
    this.rectangle = options.rectangle;
    this._display = options.display;
    this._dataProvider = options.dataProvider;
    this.extrudedHeight = options.dataProvider.externalData.size.depth;
    this._lengthways = new Cesium.AssociativeArray();
    this._grid = undefined;
    this._scaleZ = WeDisplayParameter.scaleZ;
    this._sectPositionX = 0.0;
    this._sectPositionY = 1.0;
  }

  get sectPositionX() {
    return this._sectPositionX;
  }

  set sectPositionX(value) {
    if (value >= 0.0 && value <= 1.0) {
      this._sectPositionX = value;
      this.setValue('sectPositionX', value);
    }
  }

  get sectPositionY() {
    return this._sectPositionY;
  }

  set sectPositionY(value) {
    if (value >= 0.0 && value <= 1.0) {
      this._sectPositionY = value;
      this.setValue('sectPositionY', value);
    }
  }

  get scaleZ() {
    return this._scaleZ;
  }

  set scaleZ(value) {
    if (this._scaleZ !== value) {
      if (this._grid) {
        this._grid.scaleZ = value;
      }
      this.setValue('scaleZ', value);
      this._scaleZ = value;
    }
  }

  setValue(pro, value) {
    for (let i = 0; i < this._lengthways.length; i++) {
      const lengthways = this._lengthways._array[i];

      if (Cesium.defined(lengthways[pro])) {
        lengthways[pro] = value;
      }
    }
  }

  add(options) {
    let primitive;

    if (Cesium.defined(options.positions)) {
      primitive = new WeLengthways({
        rectangle: this.rectangle,
        extrudedHeight: this.extrudedHeight,
        display: this._display,
        scaleZ: this.scaleZ,
        ...options,
      });
    } else {
      primitive = new WeLengthwaysInRect({
        extrudedHeight: this.extrudedHeight,
        display: this._display,
        scaleZ: this.scaleZ,
        ...options,
      });
    }

    if (!Cesium.defined(primitive.id)) {
      primitive.id = Cesium.createGuid();
    }

    this._lengthways.set(primitive.id, primitive);

    return primitive;
  }

  remove(lengthWay) {
    if (!Cesium.defined(lengthWay)) {
      return false;
    }
    const id = lengthWay instanceof WeLengthways ? lengthWay.id : lengthWay;

    const obj = this._lengthways.get(id);

    if (!Cesium.defined(obj)) {
      return false;
    }

    if (!this._lengthways.remove(id)) {
      return false;
    }

    obj.destroy();

    return true;
  }

  removeAll() {
    const succeed = this._lengthways.length > 0;
    for (let i = 0; i < this._lengthways.length; i++) {
      this._lengthways._array[i].destroy();
    }
    this._lengthways.removeAll();
    return succeed;
  }

  isDestroyed() {
    return false;
  }

  destroy() {
    this._grid = this._grid && this._grid.destroy();
    this.removeAll();

    return Cesium.destroyObject(this);
  }

  update(frameState) {
    for (let i = 0; i < this._lengthways.length; i++) {
      const primitive = this._lengthways._array[i];

      primitive.update(frameState);
    }
  }
}

export default WeLengthwaysManager;
